package afternoon;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class doudizhu {
    public static void main(String[] args) {
        ArrayList<String> poker = new ArrayList<String>();
        String[] colors = {"♠", "♣", "♦", "♥"};
        String[] numbers = {"2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"};
        poker.addAll(Arrays.asList("大王", "小王"));
        poker.addAll(Arrays.stream(numbers)
                           .flatMap(number -> Arrays.stream(colors).map(color -> color + number))
                           .collect(Collectors.toList()));

        // Assigning index values to each card
        Map<String, Integer> map = new HashMap<>();
        IntStream.range(0, poker.size()).forEach(i -> map.put(poker.get(i), i));

        System.out.println("牌组：" + poker);

        // Shuffling the deck
        Collections.shuffle(poker);
        System.out.println("洗牌后的牌组：" + poker);

        // Creating players
        Player p1 = new Player("老王");
        Player p2 = new Player("老李");
        Player p3 = new Player("老六");
        ArrayList<String> dipai = new ArrayList<>();

        // Distributing cards
        IntStream.range(0, poker.size())
                 .forEach(i -> {
                     String card = poker.get(i);
                     if (i >= 51) {
                         dipai.add(card);
                     } else if (i % 3 == 0) {
                         p1.getPoker().add(card);
                     } else if (i % 3 == 1) {
                         p2.getPoker().add(card);
                     } else if (i % 3 == 2) {
                         p3.getPoker().add(card);
                     }
                 });

        // Sorting cards for each player and dipai
        p1.getPoker().sort(Comparator.comparingInt(map::get));
        p2.getPoker().sort(Comparator.comparingInt(map::get));
        p3.getPoker().sort(Comparator.comparingInt(map::get));
        dipai.sort(Comparator.comparingInt(map::get));

        // Output
        System.out.println(p1.getName() + "的牌为:" + p1.getPoker());
        System.out.println(p2.getName() + "的牌为:" + p2.getPoker());
        System.out.println(p3.getName() + "的牌为:" + p3.getPoker());
        System.out.println("底牌为:" + dipai);
    }
}